#!/usr/bin/python3
#-*- encoding=UTF-8 -*-   
'''
AI人脸分析类
'''
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import json,os 
curPath = os.path.abspath(os.path.dirname(__file__))

urlList={"图片-人脸抠取":"http://localhost:8080/ai-platform/face/search","图片-人脸向量":"http://localhost:8080/ai-platform/server/face-vec","图片-人脸图片检测":"http://localhost:8080/ai-platform/server/face-check","nlp-文本向量":"http://localhost:8080/ai-platform/nlp/bert","nlp-实体识别":"http://localhost:8080/ai-platform/nlp/ner"}

defaultValue={"图片-人脸抠取":"{\"url\":\"/data/face.jpg\"}","图片-人脸向量":"{\"url\":\"/data/face/test.jpg\"}","图片-人脸图片检测":"{\"url\":\"/data/face/test.jpg\"}","nlp-文本向量":"{\"msg\":\"今天比昨天更好\"}","nlp-实体识别":"{\"msg\":\"称2011年1月17日8时许将一辆电动车（黑色松吉牌，2010年6月购买二手车，购价900元，车架号，电机号不详）停在白沙大道那洪镇国土所前印刷厂电动车停放处。\"}"}

keyList=[]
for key in urlList:
    keyList.append(key)


fontSize=10
startRow=0

class show():
    def __init__(self, root,log):
        '''
        初始化类
        '''
        self.log=log
        self.layout(root)

    def layout(self,root):
        self.control=Frame(root,bg='#F0F0F0')
        #left-左边
        fm2_left=Frame(self.control)

        #left接口类型
        num=self.getRowNum()
        Label(fm2_left,text='接口类型:',font=('微软雅黑',fontSize),width='10').grid(row=num,pady=10)
        comvalue=StringVar()#窗体自带的文本，新建一个值
        self.CmbxUrl=ttk.Combobox(fm2_left,textvariable=comvalue,width='20')
        self.CmbxUrl["values"]=keyList
        self.CmbxUrl.current(0)  #选择第一个
        self.CmbxUrl.bind("<<ComboboxSelected>>", self.boxSelect)
        self.CmbxUrl.grid(row=num,column=1,sticky=W)

        #left请求类型
        num=self.getRowNum()
        Label(fm2_left,text='类型:',font=('微软雅黑',fontSize),width='10').grid(row=num,pady=10)
        self.ButtonList = IntVar()  #管理单选按钮
        Radiobutton(fm2_left,variable=self.ButtonList,text="无",font=('微软雅黑',fontSize),width='20',value=-1,command=lambda:self.callRadiobutton(-1)).grid(row=num,column=1,sticky=W)
        Radiobutton(fm2_left,variable=self.ButtonList,text="本地文件",font=('微软雅黑',fontSize),width='20',value=0,command=lambda:self.callRadiobutton(0)).grid(row=num,column=2,sticky=W)
        Radiobutton(fm2_left,variable=self.ButtonList,text="服务器文件",font=('微软雅黑',fontSize),width='20',value=1,command=lambda:self.callRadiobutton(1)).grid(row=num,column=3,sticky=W)
        self.ButtonList.set(-1)

        #left 参数
        num=self.getRowNum()
        Label(fm2_left,text='参数:',font=('微软雅黑',fontSize),width='10').grid(row=num,pady=10)
        self.iptParam=Entry(fm2_left,width='50',font=('微软雅黑',fontSize))
        self.iptParam.grid(row=num,column=1,columnspan=2,sticky=W)
        ttk.Button(fm2_left,text='选择',command=self.choseFile).grid(row=num,column=3,sticky=W,padx=1)
        fm2_left.pack(side=LEFT,expand=YES)

        #right 右边
        fm2_right=Frame(self.control)
        self.btnSend=Button(fm2_right,text='查询',font=('微软雅黑',fontSize),width='10',command=self.send)
        self.btnSend.pack(side=TOP)
        self.btnSend=Button(fm2_right,text='查询全部',font=('微软雅黑',fontSize),width='10',command=self.sendAll)
        self.btnSend.pack()
        fm2_right.pack(side=RIGHT,expand=YES)

        self.control.pack(side=TOP,expand=YES)

        # 输出框
        fm3=Frame(root)
        self.txtMsg = Text(fm3)
        self.txtMsg.pack(side=BOTTOM,pady=40,padx=20,fill="x")
        fm3.pack(side=BOTTOM,fill="x")

    def choseFile(self):
        '''
        选择txt文件
        '''
        global curPath
        result=filedialog.askopenfilename(title='打开单个文件',filetypes=[("文本文件", "*.jpg"), ('Python源文件', '*.*')], initialdir=curPath)
        if len(result)>0:
            self.iptParam.delete(0,END)
            self.iptParam.insert(0,result)
            self.txtMsg.insert(1.0,result+"\n")
            curPath = os.path.dirname(result)
            self.ButtonList.set(0)
    
    def getRowNum(self):
        '''
        获取行号
        '''
        global startRow
        startRow+=1
        return startRow

    def callRadiobutton(self,value):
        '''
        文件类型选择
        '''
        self.ButtonList.set(value)

    def boxSelect(self,*args):
        self.iptParam.delete(0,END)
        param=defaultValue.get(self.CmbxUrl.get())

        self.iptParam.insert(0,param)

    def sendAll(self):
        '''
        测试所有接口
        '''
        self.txtMsg.delete(1.0,END)
        for title in urlList:
            url=urlList.get(title)
            param= defaultValue.get(title)
            self.txtMsg.insert(1.0,url+" "+param+"\n")
            # result=httpUtil.sendPost(url,param) #请求
            # self.txtMsg.insert(1.0,result+"\n")
            self.txtMsg.insert(1.0,"返回\n")


    def cleart(self):
        '''
        清空面板
        '''
        
        for widget in self.control.winfo_children():
            widget.destroy()

    def send(self):
        '''
        发送请求到服务器
        '''
        self.txtMsg.delete(1.0,END)
        url=self.CmbxUrl.get()

        if not url:
            self.txtMsg.insert(1.0,"异常：请求类型为空\n")
            return
        
        if url in urlList:
            url=urlList.get(url)
        else:
            self.txtMsg.insert(1.0,"异常：请求地址为空\n")
            return

        type=self.ButtonList.get()
        if type==0:
            self.txtMsg.insert(1.0,"base64\n")
        elif type ==1:
            self.txtMsg.insert(1.0,"file\n")
        
        param=self.iptParam.get()
        if not param:
            self.txtMsg.insert(1.0,"异常：参数为空\n")
            return
        # result=httpUtil.sendPost(url,param)
        result="实例"
        self.txtMsg.insert(1.0,result)

        
        

